Ext.ns('Tm.common');

Tm.common.TreeComboBox = Ext.extend(Ext.form.ComboBox,
				{
					// ----------------------属性-------------------------------------:
					/**
					 * @cfg 默认宽度
					 * @type String
					 */
					width : 250,
					/**
					 * @cfg 值为空时显示的文字
					 * @type String
					 */
					emptyText : '请选择...',
					/**
					 * @cfg 查询时显示的文字
					 * @type String
					 */
					loadingText : '查询中...',

					mode : 'local',

					store : new Ext.data.SimpleStore({
						fields : [],
						data : [ [] ]
					}),
					editable : false,
					triggerAction : 'all',
					maxHeight : 200,
					 selectedClass : '',
					forceSelection : false,
					selectOnFocus : false,
					onSelect : Ext.emptyFn,
					dataUrl:null,
					tree:null,
					disableNodeList:[],
					initComponent : function() {

						
						   this.divId = 'tree-' + Ext.id();  

					       if (isNaN(this.maxHeight))

					         this.maxHeight = 200;

					       Ext.apply(this, {  

					             tpl : '<tpl>' + '<div style="height:' + this.maxHeight + 'px;">' + '<div id="' + this.divId + '"></div>' + '</div></tpl>'  

					           });  
						
						
						
						
						 this.tree = new Ext.tree.TreePanel({
				 
 								frame : false,
								animate : false,
  								containerScroll : true,
  								rootVisible:true,
								autoWidth : true,
								height : this.maxHeight,
								split : true,
 								autoScroll : true,
								//margins : '5 0 5 5',
								listeners : {
									  scope : this,
									click : function(node) {
										if (node.id) {
 											this.setValue(node.id);
										}
									},
									 expandnode: function(node) {
										 
										 	if (this.disableNodeList.length>0&&this.disableNodeList.indexOf(node.parentNode.id)!=-1){
										 		
										 		node.disable();
										 		this.disableNodeList[this.disableNodeList.length]=node.id;
										 		return;
										 	}
										 
										 
											if (this.filterNodeId &&this.filterNodeId.toString()==node.id) {
												node.disable();
												this.disableNodeList[this.disableNodeList.length]=node.id;
												return;
											}
							 
						 }
						 
						 
						 
						 
					
						 
								},

								loader : new Ext.tree.TreeLoader({
									dataUrl : this.dataUrl

								}
								

								) 

							});   
							var root = new Ext.tree.AsyncTreeNode({
								id:1,
								text : '目录',
								draggable : false,
								//checked : false

							});
							this.tree.setRootNode(root);
						

						Tm.common.TreeComboBox.superclass.initComponent.call(this);

					},
					setNodeById : function(rootnode, nodeId) {
						var childnodes=null;
						if(rootnode.attributes&&rootnode.attributes.children&&rootnode.attributes.children.length>0){
							  childnodes =rootnode.attributes.children;
						}else if(rootnode.children&&rootnode.children.length>0){

							  childnodes = rootnode.children;
						}else if(rootnode.childNodes&&rootnode.childNodes.length>0){

							  childnodes = rootnode.childNodes;
						}else{
							//console.log(rootnode.id+'is not the path!');
						}
						
						for ( var i = 0; childnodes&&i < childnodes.length; i++) {
					
							
							
							
							
							var node = childnodes[i];
							if(node.id==nodeId){
								this.setNode( node);

							}else  {
								//console.log(node.id+'is try now!');
								this.setNodeById(node, nodeId);
							}
						}
					
					},
					setValue : function(v) {
						if(!v){
							this.lastSelectionText = null;
							if (this.hiddenField) {
								this.hiddenField.value = null;
							}
							Tm.common.TreeComboBox.superclass.setValue.call(this, null);
							
						    this.setRawValue(null);
						    this.value = null;
						    return;
						}
						
 						v = (v && v.toString) ? v.toString() : v;
 						var text = '默认';
						if (this.tree.root.childNodes && this.tree.root.childNodes.length > 0) {

							var root = this.tree.root;

							if (v == '1') {
								this.setNode( root);
							} else {
								this.setNodeById(root, v);
							}

						} else {
							var loader = new Ext.tree.TreeLoader(
									{
										dataUrl : this.dataUrl,
										
										listeners : {
											  scope : this,

											beforeload:function (   tree,   node,   callback ) {
												//alert('beforeload');
											},
											load : function(tree, node, response) {
												//alert('load');
												var root = this.tree.root;
						
												if (v == '1') {
													this.setNode(root);
												} else {
													this.setNodeById(root, v);
												}
											}
 
										}

									});
							loader.load(this.tree.root );

						}

					},
					setNode : function( node) {
			
						
						var	text = node['text'];	
						this.lastSelectionText = text;
						if (this.hiddenField) {
							this.hiddenField.value = node.id;
						}
						Tm.common.TreeComboBox.superclass.setValue.call(this, text);
					    this.setRawValue(text);
					    this.value = node.id;
					},

					
					 onRender : function(ct, position) {

					     //继承父类的Onrender展开

						 Tm.common.TreeComboBox.superclass.onRender.call(this, ct, position);

					     //默认展开

					     this.on("expand", function() {

							if (!this.tree.rendered) {
				
								this.tree.render(this.divId);
				
								this.tree.expandAll();
				
		
				
							}
				
						}, this)  ;

					 

					     },
				});
Ext.reg('treecombobox', Tm.common.TreeComboBox);
